package com.ff.dao;

import static com.ff.mapper.ShopDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import java.util.List;
import java.util.Optional;

import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ff.mapper.ShopMapper;
import com.ff.model.Shop;

@Repository
public class ShopDao {
	@Autowired
	private ShopMapper smapper;
	public void setSmapper(ShopMapper smapper) {
		this.smapper = smapper;
	}
	public List<Shop> select_() {//查询购车所有
		SelectStatementProvider ssp=select(smapper.selectList)
				.from(shop)
				.build()
				.render(RenderingStrategies.MYBATIS3);
			List<Shop> opt=smapper.selectMany(ssp);
			return opt!=null?opt:null;
	}
	public List<Shop> select_limit(int id2, int cnt1) {//分页查询购物车
		SelectStatementProvider ssp=select(smapper.selectList)
				.from(shop)
				.limit(cnt1).offset(id2)
				.build()
				.render(RenderingStrategies.MYBATIS3);
		List<Shop> opt=smapper.selectMany(ssp);
		return opt!=null?opt:null;
	}
	public int insert11(Shop s) {//加入购物车
		return smapper.insert(s);
	}
	public Shop find11(int id) {//id查询寻购物车
		SelectStatementProvider ss=select(smapper.selectList)
				.from(shop)
				.where(sid,isEqualTo(id))
				.build()
				.render(RenderingStrategies.MYBATIS3);
		Optional<Shop> opt=smapper.selectOne(ss);
		return opt.isPresent()?opt.get():null;
	}
	public int update11(Shop s) {//修改单挑购物车信息
		UpdateStatementProvider usp=update(shop)
				.set(wid).equalTo(s.getWid())
				.set(fid).equalTo(s.getFid())
				.set(snumber).equalTo(s.getSnumber())
				.set(stat).equalTo(s.getStat())
				.where(sid,isEqualTo(s.getSid()))
				.build()
				.render(RenderingStrategies.MYBATIS3);
		int i=smapper.update(usp);
		return i;
	}
	public int delete01(int id) {//删除单挑购物车信息
		return smapper.deleteByPrimaryKey(id);
	}
	public List<Shop> find12(int uid1) {//根据用户id查询购物车数据
		SelectStatementProvider ss=select(smapper.selectList)
				.from(shop)
				.where(fid,isEqualTo(uid1))
				.and(stat, isEqualTo(0))
				.build()
				.render(RenderingStrategies.MYBATIS3);
		List<Shop> ls=smapper.selectMany(ss);
		return ls!=null?ls:null;
	}
	public Shop find13(int fid1, int id1) {
		SelectStatementProvider s=select(smapper.selectList)
									.from(shop)
									.where(fid,isEqualTo(fid1))
									.and(wid, isEqualTo(id1))
									.build()
									.render(RenderingStrategies.MYBATIS3);
		Optional<Shop> opt=smapper.selectOne(s);
		return opt.isPresent()?opt.get():null;
	}
}
